<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Mysql主主复制架构配置 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Mysql主主复制架构配置</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/MySQL/">MySQL</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;MySQL主主复制结构区别于主从复制结构。在主主复制结构中，两台服务器的任</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;何一台上面的数据库存发生了改变都会同步到另一台服务器上，这样两台服务器</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;互为主从，并且都能向外提供服务。 这就比使用主从复制具有更好的性能。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;接下来我将使用两个同样的服务器来实现这个效果：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">server1_mysql：192.168.1.108</div><div class="line">server2_mysql: 192.168.1.110</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;拓扑结构：<br>server1_mysql——-server2_mysql</p>
<h2 id="1-创建用户并授权"><a href="#1-创建用户并授权" class="headerlink" title="1.创建用户并授权"></a>1.创建用户并授权</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;server1:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; GRANT REPLICATION SLAVE ON *.* TO <span class="string">'server2'</span>@<span class="string">'192.168.1.110'</span> </div><div class="line">IDENTIFIED BY <span class="string">'server2'</span>;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;server2:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; GRANT REPLICATION SLAVE ON *.* TO <span class="string">'server1'</span>@<span class="string">'192.168.1.108'</span> </div><div class="line">IDENTIFIED BY <span class="string">'server1'</span>;</div></pre></td></tr></table></figure>
<h2 id="2-修改Mysql的主配置文件"><a href="#2-修改Mysql的主配置文件" class="headerlink" title="2.修改Mysql的主配置文件"></a>2.修改Mysql的主配置文件</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;server1:</p>
 <figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[mysqld]</div><div class="line">server-id = 10</div><div class="line"><span class="built_in">log</span>-bin = mysql-bin</div><div class="line">replicate-do-db = mydb</div><div class="line">auto-increment-increment = 2   //每次增长2</div><div class="line">auto-increment-offset = 1  //设置自动增长的字段的偏移量，即初始值为2</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动Mysql服务：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># service mysqld restart</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;server2:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[mysqld]</div><div class="line"> server-id = 20</div><div class="line"> <span class="built_in">log</span>-bin = mysql-bin</div><div class="line"> replicate-do-db = mydb</div><div class="line"> auto-increment-increment = 2  //每次增长2</div><div class="line"> auto-increment-offset = 2 //设置自动增长的字段的偏移量，即初始值为2</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动Mysql服务：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># service mysqld restart</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注：二都只有server-id不同和 auto-increment- offset不同.auto-increment-increment的值应设为整个结构中服务器的总数，本案例用到两台服务器，所以值设为2。</p>
<h2 id="3-重新启动两个服务器"><a href="#3-重新启动两个服务器" class="headerlink" title="3.重新启动两个服务器"></a>3.重新启动两个服务器</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># service mysqld restart</span></div></pre></td></tr></table></figure>
<h2 id="4-为了让两个数据库一样，我们备份其中一个数据库，然后在另一个数据库上恢复，这样是两个数据库一开始都是一样的。"><a href="#4-为了让两个数据库一样，我们备份其中一个数据库，然后在另一个数据库上恢复，这样是两个数据库一开始都是一样的。" class="headerlink" title="4.为了让两个数据库一样，我们备份其中一个数据库，然后在另一个数据库上恢复，这样是两个数据库一开始都是一样的。"></a>4.为了让两个数据库一样，我们备份其中一个数据库，然后在另一个数据库上恢复，这样是两个数据库一开始都是一样的。</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在server1上操作:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># mysqldump --databases luowei &gt; /tmp/luowei.sql</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在server2上操作：创建一个与mydb同名的空数据库</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># mysql</span></div><div class="line">    &gt; CREATE DATABASE mydb;</div><div class="line">    &gt;\q</div><div class="line"><span class="comment"># scp 192.168.1.108:/tmp/mydb.sql  ./</span></div><div class="line"><span class="comment"># mysql -uroot -p mydb &lt; /tmp/luowei.sql</span></div></pre></td></tr></table></figure>
<h2 id="5-然后两个服务器相互通告二进制日志的位置并启动复制功能："><a href="#5-然后两个服务器相互通告二进制日志的位置并启动复制功能：" class="headerlink" title="5.然后两个服务器相互通告二进制日志的位置并启动复制功能："></a>5.然后两个服务器相互通告二进制日志的位置并启动复制功能：</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在server1上：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># mysql</span></div><div class="line">   &gt; CHANGE MASTER TO</div><div class="line">   &gt; MASTER_HOST=<span class="string">'192.168.1.110'</span>,</div><div class="line">   &gt; MASTER_USER=<span class="string">'server2'</span>,</div><div class="line">   &gt; MASTER_PASSWORD=<span class="string">'server2'</span>;</div><div class="line">mysql &gt; START SLAVE;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在server2上：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># mysql</span></div><div class="line">   &gt; CHANGE MASTER TO</div><div class="line">   &gt; MASTER_HOST=<span class="string">'192.168.1.108'</span>,</div><div class="line">   &gt; MASTER_USER=<span class="string">'server1'</span>,</div><div class="line">   &gt; MASTER_PASSWORD=<span class="string">'server1'</span>;</div><div class="line">mysql &gt; START SLAVE;</div></pre></td></tr></table></figure>
<h2 id="6-查看，并验证"><a href="#6-查看，并验证" class="headerlink" title="6.查看，并验证"></a>6.查看，并验证</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;分别在两个数据库服务器上查看</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql &gt; SHOW SLAVE STATUS\G;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后查看数据库和表，你会发现内容是一样的，这样就是整个主主Mysql的架构的配置过程。</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/MySQL/27. Mysql主主复制架构配置/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
